﻿using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace LZX.Installer
{
    public class LZXAssetPost:AssetPostprocessor
    {
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
        {
            OnScriptsReloaded();
        }
        private static void OnScriptsReloaded()
        {
            if(InstallerController.LZXHasInstalled() && !InstallerController.LZXHasInited())
            {
                Assembly lzxas = AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "LZX.Main.Editor");
                Type lzx = lzxas.GetType("LZX.MEditor.Tool.LZXInstaller");
                var lzxmethod = lzx.GetMethod("Install");
                lzxmethod.Invoke(null, null);
                AssetDatabase.Refresh();
            }
            if(!InstallerController.HyBridCLRHasInstalled())
            {
                Assembly assembly = System.AppDomain.CurrentDomain.GetAssemblies()
                    .First(a => a.GetName().Name == "HybridCLR.Editor");
                Type type = assembly.GetType("HybridCLR.Editor.Installer.InstallerWindow");
                var window = EditorWindow.GetWindow(type);
                var method = type.GetMethod("InstallLocalHybridCLR", BindingFlags.NonPublic | BindingFlags.Instance);
                method.Invoke(window, null);
                AssetDatabase.Refresh();
            }
        }
    }
}